﻿using System;
using System.Collections.Generic;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Tp.Dto;
using Tp.Dto.Enums;
using Tp.Dto.EventArgs;
using Tp.Interfaces.Commands.Projects;
using Tp.Interfaces.Services;
using Tp.Interfaces.ViewModels.Projects;

namespace Tp.ProjekteViewModel
{
	  public class EmployeeUserControlViewModel : ViewModelBase, IEmployeeUserControlViewModel
	  {

			#region Fields

			private IEmployeeService _service;
			private List<EmployeeDto> _employeeList;
			private EmployeeDto _selectedEmployee;

			#endregion

			#region Properties

			public IEmployeeViewModelCommands Commands { get; set; }

			public List<EmployeeDto> EmployeeList
			{
				  get { return this._employeeList; }
				  set
				  {
						if (this._employeeList != null && this._employeeList == value)
						{
							  return;
						}

						this._employeeList = value;
						RaisePropertyChanged(() => EmployeeList);
				  }
			}

			public EmployeeDto SelectedEmployee
			{
				  get { return this._selectedEmployee; }
				  set
				  {
						if (this._selectedEmployee == value)
						{
							  return;
						}

						this._selectedEmployee = value;
						RaisePropertyChanged(() => SelectedEmployee);
				  }
			}

			#endregion

			#region C-Tor

			public EmployeeUserControlViewModel(IEmployeeViewModelCommands comands, IEmployeeService service)
			{
				  this.Commands = comands;
				  this._service = service;
				  this.RegisterMessengers();
			}

			#endregion

			#region Messengers

			private void RegisterMessengers()
			{
				  Messenger.Default.Register<ProjectsOperationEnum.OperationsState>(this, ExecuteMessage);
			}

			#endregion

			#region Private methods

			private void ExecuteMessage(ProjectsOperationEnum.OperationsState state)
			{
				  switch (state)
				  {
						case ProjectsOperationEnum.OperationsState.LoadEmployee:
							  this.LoadEmployees();
							  break;
						default:
							  throw new NotImplementedException(string.Format("Message of type '{0}' is not implemented"));
				  }
			}

			private void LoadEmployees()
			{
				  EmployeeList = this._service.LoadEmployees();
			}

			#endregion

	  }
}
